import { defineStore} from 'pinia'
import { reqLogin, reqUserInfo, reqLogout } from '@/api/user/index'
import type { loginForm, responseData, userInfo } from '@/api/user/type'
import type { UserState } from './types/types'
import { reactive } from 'vue'
import { 
  SET_TOKEN, 
  GET_TOKEN, 
  GET_USERNAME,
  SET_USERNAME, 
  SET_AVATAR, 
  GET_AVATAR, 
  REMOVE_TOKEN,
  SET_PHONE,
  GET_PHONE,
  SET_EMAIL,
  GET_EMAIL,
  SET_PART,
  GET_PART,
  SET_POSITION,
  GET_POSITION,
  SET_MENU,
  GET_MENU
} from '@/utils/token'
import { aaaRoute } from '@/router/aaa'
import { bbbRoute } from '@/router/bbb'
import { adminRoute } from '@/router/admin'
import { reqTest } from '@/api/user/index'
let UseUserStore = defineStore('User', () =>{
  let user: UserState = reactive({
    token: GET_TOKEN(),
    menu: GET_USERNAME() === 'admin' ? adminRoute : GET_MENU(),
    userName: GET_USERNAME(),
    avatar: GET_AVATAR(),
    phone: GET_PHONE(),
    email: GET_EMAIL(),
    part: GET_PART(),
    position: GET_POSITION()
  })

  const userLogin_ = async (data: loginForm) => {
    let obj = {
        PageIndex: 0,
        PageSize: 0,
        Data: {
          EquipmentCategory: "string",
          EquipmentType: data.username,
          Check1: 1,
          ProductNo: data.password,
          cboArea: "string",
          Check2: 0
        }
      }
    let res:responseData<any> = await reqLogin(obj)
    if(res.Data) {
      return 'ok'
    } else {
      return Promise.reject(new Error('登录失败'))
    }
  }
  const userLogin = async (data: loginForm) => {
    let res = null
    let obj = {PageIndex: 0, PageSize: 0, Data: {EquipmentCategory: data.username, EquipmentType: data.username, Check1: 1, ProductNo: '', cboArea: '', Check2: 0}}
    let resLogin = await userLogin_(data)
    if (resLogin) {
      if (data.username !== 'admin') { res = await reqTest(obj), SET_MENU(res.Data) }
      user.token = 'gsdfghdghsdajhdhagdahsdgashdgsagdasdasgdhg'
      SET_TOKEN(user.token as string)
      SET_USERNAME(data.username)
      return 'ok'
    }
    
  }
  // const userInfo1 = async () => {
  //   let res:responseData<userInfo> = await reqUserInfo()
  //   if (res.code === 200) {
  //     user.userName = res.data.name
  //     user.avatar = res.data.avatar
  //     user.phone = '19965278665'
  //     user.email = 'test_1020@163.com'
  //     user.part = '研发部'
  //     user.position = '前端工程师'
  //     SET_USERNAME(res.data.name)
  //     SET_AVATAR(res.data.avatar)
  //     SET_PHONE('19965278665')
  //     SET_EMAIL('test_1020@163.com')
  //     SET_PART('研发部')
  //     SET_POSITION('前端工程师')
  //     return 'ok'
  //   } else {
  //     return Promise.reject(new Error('获取用户信息失败'))
  //   }
  // }
  const userInfo = async () => {
    user.userName = GET_USERNAME()
    user.avatar = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
    user.phone = '19965278665'
    user.email = 'admin@zenfocuscorp.com'
    user.part = '研发部'
    user.position = '前端工程师'
    user.menu = user.userName === 'admin' ? adminRoute : GET_MENU()
    SET_USERNAME(user.userName as string)
    SET_AVATAR(user.avatar)
    SET_PHONE('19965278665')
    SET_EMAIL('test_1020@163.com')
    SET_PART('研发部')
    SET_POSITION('前端工程师')
    return 'ok'
  }
  const userLogOut = async () => {
    // let res: responseData<any> = await reqLogout()
    // if (res.code === 200) {
    user.token = ''
    user.avatar = ''
    user.userName = ''
    REMOVE_TOKEN()
    return 'ok'
    // } else {
    //   return Promise.reject(new Error('退出登录失败'))
    // }
  }
  return {
    user,
    userLogin,
    userInfo,
    userLogOut
  }
})

export default UseUserStore
